//
//  SLVistorView.swift
//  0319iOSweiboDemo
//

//  Copyright © 2016年 大天朝. All rights reserved.
//

import UIKit

//1.定义协议.继承代理协议NSObjectProtocol
//当协议方法定义为optional时  协议需要加上@objc
@objc protocol SLVistorViewDelegate: NSObjectProtocol {
    //2.定义方法
    //点击注册按钮
   optional func visitorViewDidCilickRegisterButton()
    
    //点击登录按钮
    func visitorViewDidCilickLoginButton()
}

class SLVistorView: UIView {

    //3.定义代理属性  OC: id <SLVistorViewDelegate>
    //代理属性需要设置weak  防止被循环引用
   weak var delegate: SLVistorViewDelegate?
    
    
    //MARK: - 构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        perparUI()
       
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: 按钮点击事件
    func didClickRegisterButton() {
        //4.调用代理方法
        delegate?.visitorViewDidCilickRegisterButton?()
        
        //方法定义了optional.需要在方法前加个?判断  ?:有就继续往下执行.没有就 不执行
        //delegate?.visitorViewDidCilickRegisterButton?()
    }
    
    func didClickLoginButton() {
        //4.调用代理方法
        delegate?.visitorViewDidCilickLoginButton()
    }
    
    /**
     设置访客视图
     
     :param: imageName 图片名称
     :param: message   文字
     */
    func setupVisitor(imageName: String, message: String) {
        homeView.hidden = true  //隐藏中间的房子
        
        //父控件将子控件推到最底部
        self.sendSubviewToBack(coverView)
        
        iconView.image = UIImage(named: imageName)
        messageLabel.text = message
    }
    
    //开始旋转
    func startRotation() {
        let rotation = CABasicAnimation(keyPath: "transform.rotation")
        rotation.toValue = M_PI * 2
        rotation.duration = 20
        rotation.repeatCount = MAXFLOAT
        
        //完成时不要移除(停止)
        rotation.removedOnCompletion = false
        //添加到图层上面
        iconView.layer.addAnimation(rotation, forKey: nil)
    }
    
    //MARK: - 准备UI
    private func perparUI() {
        //背景颜色
        backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
        
        //添加子控件
        addSubview(iconView)
        //遮罩视图在轮转图上面.其他图的下面
        addSubview(coverView)
        addSubview(homeView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //设置布局
        iconView.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        
        //iconView X方向
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        //iconView Y方向
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        
        
        //遮盖的约束
        //X
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
        //Y  顶部和父控件顶部重合
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        //底部和registerButton重合
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
        
        //房子 X方向
       addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        //Y 方向
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        //消息  centenX 和 iconView.CentenX重合.宽224
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        //顶部距离iconView底部16
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        //消息宽  如果不参照其他控件toItem填nil. attribute填: NSLayoutAttribute.NotAnAttribute
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 224))

        //注册按钮.顶部距离messageLabel 16
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
       
        //左边和messageLabel的左边重合
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        
        //高度100
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        
        //高度35
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))

        //登录按钮
        //顶部距离messageLabel 16
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        //右边和messageLabel的右边重合
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        
        //高度100
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        
        //高度35
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        

    }

    //MARK: - 控件懒加载
    //轮转图
    private lazy var iconView: UIImageView = {
        let image = UIImage(named: "visitordiscover_feed_image_smallicon")
        return UIImageView(image: image)
    }()
    
    //遮罩视图
    private lazy var coverView: UIImageView = {
        let image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        return UIImageView(image: image)
    }()
    
    //房子
    private lazy var homeView: UIImageView = {
        let image = UIImage(named: "visitordiscover_feed_image_house")
        return UIImageView(image: image)
    }()
    
    //文字标签
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.text = "关注一些人,看看有什么惊喜!"
        label.textAlignment = NSTextAlignment.Center
        label.font = UIFont.systemFontOfSize(16)
        label.textColor = UIColor.darkGrayColor()
        label.numberOfLines = 0
        label.sizeToFit()
        return label
    }()
    
    //注册按钮
    private lazy var registerButton: UIButton = {
        let button = UIButton()
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        //添加点击事件
        button.addTarget(self, action: "didClickRegisterButton", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    //登录按钮
    private lazy var loginButton: UIButton = {
        let button = UIButton()
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        //添加点击事件
        button.addTarget(self, action: "didClickLoginButton", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
}
